<?php

require_once ('common.php');
checkLogin();
$id = trim($_POST['id']);
$type = trim($_POST['type']);
$action = trim($_POST['action']);
$session = trim($_POST['session']);
if (empty($id) || empty($type) || empty($action) || $session != $user['session_id'])
{
    die(json_encode(array('error' => 'invalid request')));
}
if ($action == 'quote')
{
    $request = ($type == 'topic') ? $db->getRow('SELECT * FROM bbs_topics WHERE topic_id = %1', array($id)) : $db->getRow('SELECT * FROM bbs_posts WHERE post_id = %1', array($id));
    die(json_encode($request));
}
if ($action == 'gift')
{
    $request = ($type == 'topic') ? $db->getRow('SELECT * FROM bbs_topics WHERE topic_id = %1', array($id)) : $db->getRow('SELECT * FROM bbs_posts WHERE post_id = %1', array($id));
    $member = ($type == 'topic') ? $db->getRow('SELECT gift FROM bbs_members WHERE uid = %1', array($request['topic_poster'])) : $db->getRow('SELECT gift FROM bbs_members WHERE uid = %1', array($request['poster_id']));
    if (in_array($uid, explode(',', $request['gift'])))
    {
        die(json_encode(array('status' => 'error', 'message' => 'ผิดพลาด! คุณให้กิ้ฟท์นี้ไปแล้ว')));
    }
    if ($type == 'topic')
    {
        if ($request['topic_poster'] == $uid)
        {
            die(json_encode(array('status' => 'error', 'message' => 'ผิดพลาด! ไม่สามารถให้กิ๊ฟท์ตัวเองได้')));
        }
        $gift = empty($request['gift']) ? $uid : $request['gift'] . ',' . $uid;
        $db->updateRow('bbs_topics', array('topic_id' => $id), array('gift' => $gift));
        $db->updateRow('bbs_members', array('uid' => $request['topic_poster']), array('gift' => $member['gift'] + 1));
        $result = '<br /><br /><img src="' . APP_URL . '/images/gift-16.png" align="top" /><span style="color:#588000">' . count(explode(',', $gift)) . ' กิ้ฟท์โดย: </span>';
        foreach (explode(',', $gift) as $r => $t)
        {
            $profile = getUserProfile($t);
            $sep = ($r == 0) ? '' : ', ';
            $result .= $sep . '<a href="' . APP_URL . '/user/' . $profile['uid'] . '">' . $profile['realname'] . '</a>';
        }
        die(json_encode(array('status' => 'success', 'message' => $result)));
    }
    else
    {
        if ($request['poster_id'] == $uid)
        {
            die(json_encode(array('status' => 'error', 'message' => 'ผิดพลาด! ไม่สามารถให้กิ๊ฟท์ตัวเองได้')));
        }
        $gift = empty($request['gift']) ? $uid : $request['gift'] . ',' . $uid;
        $db->updateRow('bbs_posts', array('post_id' => $id), array('gift' => $gift));
        $db->updateRow('bbs_members', array('uid' => $request['poster_id']), array('gift' => $member['gift'] + 1));
        $result = '<br /><br /><img src="' . APP_URL . '/images/gift-16.png" align="top" /><span style="color:#588000"> ' . count(explode(',', $gift)) . ' กิ้ฟท์โดย: </span>';
        foreach (explode(',', $gift) as $r => $t)
        {
            $profile = getUserProfile($t);
            $sep = ($r == 0) ? '' : ', ';
            $result .= $sep . '<a href="' . APP_URL . '/user/' . $profile['uid'] . '">' . $profile['realname'] . '</a>';
        }
        die(json_encode(array('status' => 'success', 'message' => $result)));
    }
}
if ($action == 'preview')
{
    $message = trim(htmlspecialchars($_POST['text']));
    $message = $bbcode->coverpass($message);
    die(json_encode(array('status' => 'success', 'html' => $message)));
}

?>